<?php
/*
 * $connectedList = $this->connectedLists('connected lists demo');
 * $connectedList->setCallbackUrl('url to post the serialized list back to');
 * $connectedList->setUnselectedId('unselectedItems');
 * $connectedList->addUnselectedItem(1,'test1');
 * $connectedList->addUnselectedItem(2,'test2');
 * $connectedList->addUnselectedItem(3,'test3');
 * $connectedList->setSelectedId('selectedItems');
 * $connectedList->addSelectedItem(4,'test4');
 * $connectedList->addSelectedItem(5,'test5');
 * $connectedList->addSelectedItem(6,'test6');
 * echo $connectedList->render();
 */
class Digitalus_View_Helper_ConnectedLists extends Zend_View_Helper_HtmlElement 
{
    public $options;
    public $label;
    public $postKey = 'item';
    public $connectorClass = 'connectSortable';
    public $unselectedItems;
    public $unselectedOptions;
    public $selectedItems;
    public $selectedOptions;
    public $callbackUrl = null;
    public $callbackFunction = null;
    
    public function connectedLists($label, $options = null)
    {
        $this->label = $label;
        $this->options = $options;
        return $this;
    }
    
    public function setUnselectedList($itemsArray = array(), $options = null)
    {
        $this->unselectedItems = $itemsArray;
        $this->unselectedOptions = $options;
    }
    
    public function setUnselectedId($id)
    {
        $this->unselectedOptions['id'] = $id;
    }
    
    public function addUnselectedItem($id, $item) 
    {
        $this->unselectedItems[$id] = $item;
    }
    
    public function setSelectedId($id)
    {
        $this->selectedOptions['id'] = $id;
    }
    
    public function addSelectedItem($id, $item) 
    {
        $this->selectedItems[$id] = $item;
    }
    
    public function setSelectedList($itemsArray = array(), $options = null)
    {
        $this->selectedItems = $itemsArray;
        $this->selectedOptions = $options;
    }
    
    public function setCallbackUrl($url)
    {
        $this->callbackUrl = $url;
    }
    
    public function setCallbackFunction($function)
    {
        $this->callbackFunction = $function;
    }
    
    public function getCallbackFunction()
    {
        if($this->callbackUrl != null) {
            return "$.post('{$this->callbackUrl}', selectedItems);";
        }elseif ($this->callbackFunction != null) {
            return $this->callbackFunction . "(selectedItems)";
        }
    }
    
    public function render($asXhtml = true)
    {
        // validate data
        $this->_validate();
        // add jquery
        $this->_addJquery();
        
        // add connector class
        if(isset($this->unselectedOptions['class'])) {
            $this->unselectedOptions['class'] .= ' ' . $this->connectorClass;
        } else {
            $this->unselectedOptions['class'] = $this->connectorClass;
        }
        if(isset($this->selectedOptions['class'])) {
            $this->selectedOptions['class'] .= ' ' . $this->connectorClass;
        } else {
            $this->selectedOptions['class'] = $this->connectorClass;
        }
        $lists = new stdClass();
        $lists->unselected = $this->_renderList($this->unselectedItems, $this->unselectedOptions, 'ui-state-default');
        $lists->selected = $this->_renderList($this->selectedItems, $this->selectedOptions, 'ui-state-highlight');
        
        if($asXhtml) {
            // render the control
            $xhtml = $lists->unselected;
            $xhtml .= $lists->selected;
            
            return $xhtml;
        }else{
            return $lists;
        }
    }
    
    protected function _addJquery()
    {
        if(!$this->view->jQuery()->isEnabled()) {
            $this->view->jQuery()->enable();
        }
        if(!$this->view->jQuery()->uiIsEnabled()) {
            $this->view->jQuery()->uiEnable();
        }
        $this->view->jQuery()->addOnLoad("
            $('#{$this->unselectedOptions['id']}, #{$this->selectedOptions['id']}').sortable({
    			connectWith: '.{$this->connectorClass}',
    			stop: function(event, ui){
    				var selectedItems = $('#{$this->selectedOptions['id']}').sortable('serialize');
    				" . $this->getCallbackFunction() . "
    			}
    		}).disableSelection();
        ");
        
    }
    
    protected function _validate()
    {
        if(!isset($this->unselectedOptions['id'])) {
            throw new Zend_Exception("You must set the id of the unselected list for the connected lists control to function");
        }
        if(!isset($this->selectedOptions['id'])) {
            throw new Zend_Exception("You must set the id of the selected list for the connected lists control to function");
        }
    }
    
    protected function _renderList($items, $options, $itemClass = null)
    {
         $xhtml = "<ul " . $this->_htmlAttribs($options) . ">" . PHP_EOL;
         if(count($items) > 0) {
             foreach ($items as $id => $item) {
                 $xhtml .= $this->_renderListItem($id, $item, $itemClass);
             }
             
         }
         $xhtml .= "</ul>" . PHP_EOL;
         return $xhtml;
    }
    
    protected function _renderListItem($id, $item, $itemClass = null)
    {
        return "<li id='" . $this->postKey . "_" . $id . "' class='" . $itemClass . "'>" . $item . "</li>" . PHP_EOL;
    }
}
?>